package org.csu.jpetstoreagain.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.jpetstoreagain.domain.Category;
import org.csu.jpetstoreagain.domain.Inventory;
import org.csu.jpetstoreagain.domain.Item;
import org.csu.jpetstoreagain.domain.Product;
import org.csu.jpetstoreagain.persistence.CategoryMapper;
import org.csu.jpetstoreagain.persistence.InventoryMapper;
import org.csu.jpetstoreagain.persistence.ItemMapper;
import org.csu.jpetstoreagain.persistence.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CatalogService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private InventoryMapper inventoryMapper;

    public List<Category> getCategoryList() {

        return categoryMapper.selectList(null);
    }

    //    根据id查category
    public Category getCategory(String categoryId) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("catid", categoryId);
        return categoryMapper.selectOne(wrapper);
    }


    //根据productid查product
    public Product getProduct(String productId) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("productid", productId);
        return productMapper.selectOne(wrapper);
    }

    //根据categoryid查product
    public List<Product> getProductListByCategory(String categoryId) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("category", categoryId);
        return productMapper.selectList(wrapper);
    }

    // TODO enable using more than one keyword
    //模糊查询 使用名字搜索
    public List<Product> searchProductList(String keyword) {
        return productMapper.searchProductList(keyword);
    }

    //根据produtid找item
    public List<Item> getItemListByProduct(String productId) {
        QueryWrapper<Item> wrapper = new QueryWrapper<>();
        wrapper.eq("productid", productId);
        return itemMapper.selectList(wrapper);
    }

    //根据itemid找item
    public Item getItem(String itemId) {
        QueryWrapper<Item> wrapper = new QueryWrapper<>();
        wrapper.eq("itemid", itemId);
        return itemMapper.selectOne(wrapper);
    }

    //根据itemid找quantity（数据库表inventory）
    public Inventory getItemQuantityByItemId(String itemId){
        QueryWrapper<Inventory> wrapper = new QueryWrapper<>();
        wrapper.eq("itemid", itemId);
        return inventoryMapper.selectOne(wrapper);
    }


    public boolean isItemInStock(String itemId) {
        //库存永远大于0
        return true;

    }
}
